# Copyright (c) 2019 Presto Labs Pte. Ltd.
# Author: yuxuan

import os
from collections import namedtuple

FillInfo = namedtuple(
    'FillInfo',
    ['action', 'symbol', 'exchange', 'fill_price', 'fill_qty', 'order_side', 'timestamp'])


class FillInfoWriter(object):
  def __init__(self, path, strategy_name):
    self._path = path
    self._strategy_name = strategy_name
    if not os.path.exists(self._path):
      with open(self._path, 'w') as fo:
        fo.write('action,symbol,exchange,fill_price,fill_qty,order_side,timestamp,strategy_name\n')

  def write(self, fillinfo):
    with open(self._path, 'a') as fo:
      fo.write('%s,%s,%s,%s,%s,%s,%s,%s\n' % (fillinfo.action,
                                              fillinfo.symbol,
                                              fillinfo.exchange,
                                              fillinfo.fill_price,
                                              fillinfo.fill_qty,
                                              fillinfo.order_side,
                                              fillinfo.timestamp,
                                              self._strategy_name))
